
//tslint:disable
import { Component, ViewChild, ComponentFactoryResolver, Input, OnInit, ViewContainerRef, SimpleChanges, NgZone } from '@angular/core';
import { TabsService } from 'src/app/shared/service/tabs.service';
import { EventService } from '../../shared/service/event.service';
import { ComItem } from './com-item';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
@Component({ selector: 'app-footer-coms', template: '<section>\n' + '<ng-template #footerComsAnchor></ng-template>\n' + '</section>' })
export class FooterComsComponent implements OnInit {
    @Input() coms: ComItem[];
    @ViewChild('footerComsAnchor', { read: ViewContainerRef }) myRef: ViewContainerRef;
    constructor(
        private componentFactoryResolver: ComponentFactoryResolver,
        private eventService: EventService,
        private tabService: TabsService,
        public appGlobalService: AppGlobalService,
        private ngZone: NgZone) {
            
        this.eventService.eventEmit.on("clearFooterComs", (data) => {
            const refreshFooterTag = localStorage.getItem("clearFooterComsTag");
            if (refreshFooterTag !== null) {
                localStorage.removeItem('clearFooterComsTag');
                const viewContainerRef = this.myRef;
                viewContainerRef.clear();
            }
        });
    }
    ngOnInit() {
        this.ngZone.runOutsideAngular(() => {
            this.ngZone.run(() => {
                this.eventService.eventEmit.on("loadFooterComponent", (data) => {
                    this.coms = data;
                });
            });
        });
        if (this.appGlobalService.eventService.eventEmit.listenerCount("updateTagsDataFooter" + localStorage.getItem('currentPath')) > 0) {
            this.appGlobalService.eventService.eventEmit.removeListener("updateTagsDataFooter" + localStorage.getItem('currentPath'));
          }
        this.appGlobalService.eventService.eventEmit.on("updateTagsDataFooter" + localStorage.getItem('currentPath'),res =>{
            this.updateTagsData();
        });
    }
    ngOnChanges(changes: SimpleChanges): void {
        if (this.coms) {
            const viewContainerRef = this.myRef;
            viewContainerRef.clear();
            this.coms.forEach((v) => {
                const componentFactory = this.componentFactoryResolver.resolveComponentFactory(v.component);
                const componentRef = viewContainerRef.createComponent(componentFactory);
                (componentRef.instance).tagData = v.tagData;
            });
        }
    }
    loadComponent(askTag) {
        this.coms = [];
        this.myRef.clear();
        const tag = JSON.parse(askTag);
        if (!tag.clean) {
            this.tabService.getComsDataByStyle(tag.style, callData => { if (callData) { this.coms.push(callData); } }, tag);
            const len = this.coms.length;
            for (let i = 0; i < len; i++) {
                const v = this.coms[i];
                const factory = this.componentFactoryResolver.resolveComponentFactory(v.component);
                const ref = this.myRef.createComponent(factory);
                ref.instance.tagData = v.tagData;
                ref.changeDetectorRef.detectChanges();
            }
        }
        localStorage.removeItem("refreshFooterComsTag");
    }

    updateTagsData() {
        
        let temArray:any = [];

        for(let i = 0; i < this.myRef.length; i++){
            const tagInstance = this.myRef.get(i)["_view"].nodes[1].instance;
            if (tagInstance.tagData && tagInstance.tagData.shellDataId && tagInstance.tagData.shellDataId !== "0") {
                if (this.appGlobalService.getShellData(tagInstance.tagData.shellDataId)) {
                    // tagInstance.tagData = this.getDemoData();
                    if (this.appGlobalService.getShellData(tagInstance.tagData.shellDataId).remove === "true"){
                        temArray.push(this.myRef.get(i));
                    } else {
                        tagInstance.tagData = this.appGlobalService.getShellData(tagInstance.tagData.shellDataId);
                    }
                    
                  }
            }
        }
        if (temArray.length > 0 ) {
            temArray.forEach(element => {
                const index = this.myRef.indexOf(element)
                if (index >= 0) {
                    
                    this.myRef.remove(index);
                    this.appGlobalService.deleteShellData(element["_view"].nodes[1].instance.tagData.shellDataId);
                    console.log("page appAnchor  remove tag:"+ element["_view"].nodes[1].instance.tagData.shellDataId);
                }
            });
        }
    }
}
